package com.example.fireassistant_server.controller;

import com.example.fireassistant_server.dto.PointSearchFilter;
import com.example.fireassistant_server.dto.circle.CircleCreateRequest;
import com.example.fireassistant_server.dto.circle.CircleUpdateRequest;
import com.example.fireassistant_server.dto.hydrant.FireHydrantAddRequest;
import com.example.fireassistant_server.dto.hydrant.FireHydrantUpdateRequest;
import com.example.fireassistant_server.entity.User;
import com.example.fireassistant_server.mapper.CircleMapper;
import com.example.fireassistant_server.service.CircleService;
import com.example.fireassistant_server.service.UserService;
import com.example.fireassistant_server.service.WechatUserService;
import com.example.fireassistant_server.vo.CircleVo;
import com.example.fireassistant_server.vo.FireHydrantVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/circle")
public class CircleController {

    private WechatUserService wechatUserService;

    private CircleMapper circleMapper;

    private CircleService circleService;

    private UserService userService;

    @PostMapping("/add/{user_id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    CircleVo Add(@Validated @RequestBody CircleCreateRequest circleCreateRequest, @PathVariable String user_id){
        User user = userService.get(user_id);
        return circleMapper.toVo(circleService.addCircle(circleCreateRequest, user));
    }

    // 微信端添加标记线 ；普通用户可以添加
    @PostMapping("/weChat/add")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    CircleVo weAdd(@Validated @RequestBody CircleCreateRequest circleCreateRequest, @RequestHeader(value = "X-WX-OPENID") String openId){
        User user = wechatUserService.getCurrentUser(openId).getUser();
        return circleMapper.toVo(circleService.addCircle(circleCreateRequest,user));
    }

    //根据id获取圆形
    @GetMapping("/{id}")
    CircleVo get(@PathVariable String id) {
        return circleMapper.toVo(circleService.get(id));
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    CircleVo update(@PathVariable String id,
                         @Validated @RequestBody CircleUpdateRequest circleUpdateRequest) {
        return circleMapper.toVo(circleService.update(id,circleUpdateRequest));
    }

    //根据id删除线段
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    void delete(@PathVariable String id) {
        circleService.delete(id);
    }

    @PostMapping("/search")
    Page<CircleVo> search(@Validated @RequestBody PointSearchFilter pointSearchFilter) {
        return circleService.search(pointSearchFilter).map(circleMapper::toVo);
    }

    @Autowired
    public void setWechatUserService(WechatUserService wechatUserService) {
        this.wechatUserService = wechatUserService;
    }

    @Autowired
    public void setCircleMapper(CircleMapper circleMapper) {
        this.circleMapper = circleMapper;
    }

    @Autowired
    public void setCircleService(CircleService circleService) {
        this.circleService = circleService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
